﻿#include <QAxObject>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QDir>

#include "qexceldocument.h"
#include "qexceldriver.h"


QExcelDocument::QExcelDocument(QString filePath,QObject *parent) : QObject(parent)
{
    m_filePath = QDir::toNativeSeparators(filePath);

    QExcelDriver *excelApp = QExcelDriver::instance();

    if(!excelApp->isNull()){
        QFileInfo fileInfo(filePath);
        if(fileInfo.exists()){
            m_excelWorkbook = excelApp->openWorkbook(filePath);
        }else{
            m_excelWorkbook = excelApp->addWorkbook();
        }
    }else{
        m_excelWorkbook = nullptr;
    }
}

QExcelDocument::~QExcelDocument()
{
    if(m_excelWorkbook && !m_excelWorkbook->isNull()){
        m_excelWorkbook->dynamicCall("Close(bool saveChange)",false);
    }
}

bool QExcelDocument::isAvailable()
{
    if(m_excelWorkbook && !m_excelWorkbook->isNull()){
        return true;
    }else{
        return false;
    }
}

QString QExcelDocument::filePath()
{
    return m_filePath;
}

int QExcelDocument::sheetCount()
{
    if(isAvailable()){
        return m_excelWorkbook->querySubObject("Worksheets")->dynamicCall("Count").toInt();
    }else{
        return -1;
    }
}

QStringList QExcelDocument::sheetNames()
{
    if(isAvailable()){
        QAxObject *sheets = m_excelWorkbook->querySubObject("Worksheets");
        int count = sheets->dynamicCall("Count").toInt();

        QStringList nameList;
        for(int i=1;i<=count;i++){
            nameList<< sheets->querySubObject("Item(int index)",i)->dynamicCall("Name").toString();
        }

        return nameList;
    }else{
        return QStringList();
    }
}

QExcelSheet *QExcelDocument::activeSheet()
{
    if(isAvailable()){
        return new QExcelSheet(m_excelWorkbook->querySubObject("ActiveSheet"),this);
    }else{
        return nullptr;
    }
}

bool QExcelDocument::setActiveSheet(QString sheetName)
{
    if(isAvailable()){
        QAxObject *sheets = m_excelWorkbook->querySubObject("Worksheets");
        QAxObject *sheet = sheets->querySubObject("Item(QString &name)",sheetName);
        if(sheet){
            sheet->dynamicCall("Activate()");
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

bool QExcelDocument::setActiveSheet(int sheetIndex)
{
    if(isAvailable()){
        if(sheetIndex>sheetCount() || sheetIndex<1){
            return false;
        }

        QAxObject *sheets = m_excelWorkbook->querySubObject("Worksheets");
        QAxObject *sheet = sheets->querySubObject("Item(int &sheetIndex)",sheetIndex);
        if(sheet){
            sheet->dynamicCall("Activate()");
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

bool QExcelDocument::addSheet(QString sheetName)
{
    if(isAvailable()){
        if(sheetNames().contains(sheetName)){
            return false;
        };

        QAxObject *sheets = m_excelWorkbook->querySubObject("Worksheets");
        QAxObject *sheet = sheets->querySubObject("Add()");
        if(sheet){
            sheet->setProperty("Name",sheetName);
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

bool QExcelDocument::deleteSheet(int sheetIndex)
{
    if(isAvailable()){
        if(sheetIndex>sheetCount() || sheetIndex<1){
            return false;
        }

        QAxObject *sheets = m_excelWorkbook->querySubObject("Worksheets");
        QAxObject *sheet = sheets->querySubObject("Item(int &sheetIndex)",sheetIndex);
        if(sheet){
            sheet->dynamicCall("Delete()");
            return true;
        }else{
            return false;
        }

    }else{
        return false;
    }
}

bool QExcelDocument::deleteSheet(QString sheetName)
{
    if(isAvailable()){
        if(!sheetNames().contains(sheetName)){
            return false;
        };

        QAxObject *sheets = m_excelWorkbook->querySubObject("Worksheets");
        QAxObject *sheet = sheets->querySubObject("Item(QString &sheetName)",sheetName);
        if(sheet){
            sheet->dynamicCall("Delete()");
            return true;
        }else{
            return false;
        }

    }else{
        return false;
    }
}

bool QExcelDocument::deleteAllSheet()
{
    if(isAvailable()){
        QAxObject *sheets = m_excelWorkbook->querySubObject("Worksheets");
        int count = sheetCount();
        for(int i=1;i<count;i++)
        {
            QAxObject *sheet = sheets->querySubObject("Item(int &sheetIndex)",1);
            if(sheet){
                sheet->dynamicCall("Delete()");
            }
        }
        return true;
    }else{
        return false;
    }
}

bool QExcelDocument::save()
{
    if(isAvailable()){
        QFileInfo fileInfo(m_filePath);
        if(fileInfo.exists()){
            m_excelWorkbook->dynamicCall("Save()");
            return true;
        }else{
            return m_excelWorkbook->dynamicCall("SaveAs(QString path)",m_filePath).toBool();
        }
    }else{
        return false;
    }
}

bool QExcelDocument::saveAs(QString filePath)
{
    if(isAvailable()){
        QString nativeFilePath = QDir::toNativeSeparators(filePath);
        return m_excelWorkbook->dynamicCall("SaveAs(QString path)",nativeFilePath).toBool();
    }else{
        return false;
    }
}
